package com.project.server.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.project.server.dto.FamilyMemberDTO;
import com.project.server.entity.FamilyMember;
import com.project.server.entity.SysUser;
import com.project.server.mapper.FamilyMemberMapper;
import com.project.server.mapper.HealthRecordMapper;
import com.project.server.mapper.SysUserMapper;
import com.project.server.service.FamilyMemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class FamilyMemberServiceImpl implements FamilyMemberService {

    @Autowired
    private FamilyMemberMapper familyMemberMapper;

    @Autowired
    private SysUserMapper userMapper;


    @Override
    public List<Map<String, Object>> getMemberById(Long memberId) {
        List<FamilyMember> users = familyMemberMapper.selectList(
            new LambdaQueryWrapper<FamilyMember>()
                .eq(FamilyMember::getMemberId, memberId)
                .eq(FamilyMember::getStatus, 1)
        );

        List<Map<String, Object>> result = new ArrayList<>();
        for (FamilyMember memberUser : users) {
            SysUser user = userMapper.selectById(memberUser.getUserId());
            if (user == null) {
                continue;
            }
            Map<String, Object> map = new HashMap<>();
            map.put("id", user.getId());
            map.put("memberId", memberUser.getMemberId());
            map.put("realName", user.getRealName());
            map.put("relationType", memberUser.getRelationType());
            map.put("phone", user.getPhone());
            map.put("age", user.getAge());
            map.put("gender", user.getGender());
            result.add(map);
        }
        return result;
    }

    @Override
    @Transactional
    public void addMember(FamilyMemberDTO memberDTO) {
        // 检查是否已经是家庭成员
        if (familyMemberMapper.selectCount(
                new LambdaQueryWrapper<FamilyMember>()
                    .eq(FamilyMember::getUserId, memberDTO.getUserId())
                    .eq(FamilyMember::getMemberId, memberDTO.getMemberId())
                    .eq(FamilyMember::getStatus, 1)
            ) > 0) {
            throw new RuntimeException("该用户已经是您的家庭成员");
        }

        FamilyMember familyMember = new FamilyMember();
        familyMember.setUserId(memberDTO.getUserId());
        familyMember.setMemberId(memberDTO.getMemberId());
        familyMember.setRelationType(memberDTO.getRelationType());
        familyMember.setStatus(1);

        familyMemberMapper.insert(familyMember);
    }


    @Override
    @Transactional
    public void removeMember(Long userId, Long memberId) {
        SysUser user = userMapper.selectById(userId);

        FamilyMember member = familyMemberMapper.selectOne(
            new LambdaQueryWrapper<FamilyMember>()
                .eq(FamilyMember::getUserId, user.getId())
                .eq(FamilyMember::getMemberId, memberId)
        );

        if (member != null) {
            member.setStatus(0);
            familyMemberMapper.updateById(member);
        }
    }


}
